import { Component } from '@angular/core';
import {MarketingCreditListService} from "./list.service";
import {MarketingCreditQueryPage} from "../query/query.component";
import {MarketingCreditListDetailPage} from "../listDetail/listDetail.component"
import _ from 'lodash';
import {ActionSheetController, ModalController, NavController, PopoverController} from "ionic-angular";
@Component({
  selector: 'page-marketing-credit-list',
  templateUrl: './list.html',
  providers:[MarketingCreditListService]
})

export class MarketingCreditListPage {
  moa_page_name: any = "page-marketing-credit-list";
  moa_page_explain: any = "营销平台 - 诚信管理";

  isLoadMore=true;
  listData=[];
  managerName='';
  level='';
  constructor(private ls:MarketingCreditListService,
              private as:ActionSheetController,
              private ms:ModalController,
              private nav:NavController){
    this.setListData('','');
  }
  setListData(managerName?,level?,refresh?,infinite?){
      this.ls.getListData(managerName, level).subscribe(data => {
        console.log(data);
        if (data) {
          let dataList = [];
          _.forEach(data, (i) => {
            let d = i.split("|");
            dataList.push(d);
          });
          this.listData = dataList;
        }else{
          this.isLoadMore=false;
          this.listData=[];
        }
      },(err)=>{
      },()=>{
        if(refresh){
          refresh.complete();
        }
        if(infinite){
          infinite.complete();
        }
      });
  }
  search(){
    this.setListData(this.managerName,'');
  }
  goDetailPage(item){
    this.nav.push(MarketingCreditListDetailPage,{
      item:item
    })
  }
  chooseListType(){
    let actionSheet=this.as.create({
      title:'',
      buttons:[{
        text:'高级查询',
        role:'destructive',
        handler:()=>{
          this.showAdvancedQueryModal();
        }
      },
        {
          text:'取消',
          role:'cancle',
          handler:()=>{
          }
        }
      ]
    });
    actionSheet.present();
  }
  showAdvancedQueryModal(){
    let modal=this.ms.create(MarketingCreditQueryPage,{
      managerName:this.managerName,
      level:this.level
    });
    modal.onDidDismiss((data)=>{
      if(data){
        this.managerName=data.managerName||'';
        this.level=data.level||'';
        this.setListData(data.managerName,data.level);
      }
    });
    modal.present();
  }
  doRefresh(refresh){
    this.setListData(this.managerName,this.level,refresh);
  }
  doInfinite(infiniteSroll){
    //
    this.isLoadMore=false;
    infiniteSroll.complete();
  }
}


